package com.wd.tw.service;

import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wd.tw.config.AppConfig;
import com.wd.tw.domain.dto.EventQuery;
import com.wd.tw.domain.dto.response.AlarmData;
import com.wd.tw.domain.dto.response.EventData;
import com.wd.tw.domain.dto.response.ImageListResponse;
import com.wd.tw.utils.TimeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @className: AlarmService
 * @description: TODO 类描述
 * @author: liumingkun
 * @date: 2024/11/4
 **/

@Service
public class AlarmService {

    private final static Logger logger = LoggerFactory.getLogger(AlarmService.class);

    @Resource
    ImageService imageService;

    @Resource
    AppConfig appConfig;

    private static long startTime = TimeUtils.getStartTimeStamp();

    public List<AlarmData> getEventList() {
        EventQuery eventQuery = new EventQuery();
        eventQuery.setQry_len(5);
        eventQuery.setStart_no(1);

        long endTime = TimeUtils.getCurrentTime();
        String sTime = TimeUtils.parseStr(startTime);
        String eTime = TimeUtils.parseStr(endTime);
        eventQuery.setStart_time(sTime);
        eventQuery.setStop_time(eTime);

        logger.info("Query Start" + sTime);
        logger.info("Query End" + eTime);


        if (startTime >= endTime) {
            logger.info("Do not have to search!");
            List<AlarmData> alarmDatas = new ArrayList<>();
            return alarmDatas;
        }

        startTime = endTime;
        ImageListResponse event = imageService.getImageList(eventQuery);
        if (event == null) {
            return null;
        }
        List<EventData> data = event.getData();

        logger.info("response data:" + JSONObject.toJSONString(data));

        String jsonString = "{\"alarm_type\":0,\"alive_type\":0,\"appear_count\":0,\"camera_name\":\"通道1\",\"channel\":1,\"device_id\":\"ID\",\"event_type\":0,\"face_attr\":{\"cap_style\":\"hat_style_type_none\",\"gender_code\":\"male\",\"glass_style\":\"transparent_glasses\",\"mustache_style\":\"mustache_style_type_none\",\"respirator_color\":\"color_type_none\",\"st_age\":\"st_adult\",\"st_age_value\":\"33.000000\",\"st_expression\":\"st_angry\",\"st_helmet_style\":\"st_helmet_style_type_none\",\"st_respirator\":\"st_respirator_mouth\"},\"img\":\"abc\",\"img_id\":\"v214_1701621271_237_0\",\"img_path\":\"facebase/libid2/2023/12/04/v214_1701621271_237_0.png\",\"lib_id\":2,\"lib_name\":\"测试\",\"lib_type\":2,\"liveness_score\":0,\"liveness_thr\":0,\"obj_label\":1,\"ora_pos_bottom\":0,\"ora_pos_left\":0,\"ora_pos_right\":0,\"ora_pos_top\":0,\"person_addr\":\"\",\"person_age\":\"\",\"person_gender\":\"\",\"person_idcard\":\"\",\"person_name\":\"myj\",\"pos_bottom\":549,\"pos_left\":712,\"pos_right\":898,\"pos_top\":363,\"position\":\"\",\"quality\":86,\"ranking\":1,\"similarity\":36,\"snap_buf\":\"abcd\",\"snap_id\":\"v214_1730646287_8097_0\",\"snap_path\":\"record/face/channel1/2024/11/03/v214_1730646287_8097_0.jpg\",\"stranger_appear_channel\":0,\"threshold\":85,\"trigger\":\"2024-11-03 23:04:47\",\"wander_channels\":\"\",\"wander_deviceID\":\"\",\"wander_thresHold\":30,\"wander_trigger\":\"\"}"; // 替换为实际的 JSON 字符串

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        List<AlarmData> alarmDatas = new ArrayList<>();
        try {
            for (int i = 0; i < data.size(); i++) {
                EventData eventData = data.get(i);
                AlarmData alarmData = objectMapper.readValue(jsonString, AlarmData.class);
                alarmData.setImg_path("/image/edge?file=" + eventData.getTarget_image_path());
                alarmData.setSnap_path("/image/edge?file=" + eventData.getScene_image_path());
                alarmData.setPerson_name("");
                alarmData.setSimilarity(99);
                alarmData.setLib_id(9);
                alarmData.setLib_type(9);
                alarmData.setTrigger(TimeUtils.parseStr(eventData.getTimestamp()));
                alarmData.setChannel(eventData.getChannel_id());
                alarmData.setCamera_name(appConfig.getMap().get(eventData.getChannel_id()));
                alarmDatas.add(alarmData);
            }
            return alarmDatas;
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("AlarmListError" + e);
            logger.info("AlarmListError" + e.getMessage());
            logger.info("AlarmListData:" + data);
            logger.info("AlarmListQueryData" + eventQuery);
        }

        return alarmDatas;
    }
}
